export const pub = {
    data(){
        return {
            show:true,
            value:"",
            modal12: false,
            listdata: [],
            total:0,
            page:1,
            rows:10,
            titles:'',
            oldobj:{},
            arr:[],
            row:null,
            url:this.$store.state.url,
            path:window.location.href.substring(window.location.href.lastIndexOf('/')+1)
        }
      },
      methods:{
        //获取数据列表
            //请求数据函数
            post(p){
                this.axios({
                    url:`${this.url}${p}/list`,
                    method:'post',
                    data:{
                        page:this.page,
                        rows:this.rows,
                        name: this.value,
                        autor:this.value,
                        productname:this.value
                    }
                }).then(res=>{
                    this.listdata = res.data.rows;
                    this.total = res.data.total;
                })
            },
            getdata(){
                if(this.path == "users"){
                    this.post(this.path);
                }else if(this.path == "news"){
                    this.post(this.path);
                }else if(this.path == "product"){
                    this.post(this.path);
                }
            },
            //全选和取消全选
            handleSelectAll (status) {
                this.$refs.selection.selectAll(status);
            },
            //点击确定添加数据
            ok (){
                var obj = this.formItem;
                if(this.row != null){//修改数据
                    if(this.path == "users"){
                        this.axios({
                            url:`${this.url}users/${this.row}`,
                            method:'put',
                            data:obj
                        }).then( ()=>{
                            this.success(true);
                            this.getdata();
                        })
                    }else if(this.path == "news"){
                        this.axios({
                            url:`${this.url}news/${this.row}`,
                            method:'put',
                            data:obj
                        }).then( ()=>{
                            this.success(true);
                            this.getdata();
                        })
                    }else if(this.path == "product"){
                        this.axios({
                            url:`${this.url}product/${this.row}`,
                            method:'put',
                            data:obj
                        }).then( ()=>{
                            this.success(true);
                            this.getdata();
                        })
                    }else if(this.path == "vip"){
                        this.axios({
                            url:`${this.url}users/${this.row}`,
                            method:'put',
                            data:obj
                        }).then( ()=>{
                            this.success(true);
                            location.reload()
                        })
                    }
                }else{//添加数据
                    if(this.path == "users"){
                        this.adddata(this.path,obj);
                    }else if(this.path == "news"){
                        this.adddata(this.path,obj);
                    }else if(this.path == "product"){
                        this.adddata(this.path,obj);
                    }else if(this.path == "vip"){
                        this.adddata('users',obj);
                    }
                }
            },
            //添加数据函数
            adddata(p,obj){
                this.axios({
                    url:`${this.url}${p}`,
                    method:'post',
                    data:obj
                }).then( ()=>{
                    this.success(true);
                    this.getdata();
                })
            },
            success (nodesc) {
                this.$Notice.success({
                    title: '成功',
                    desc: nodesc ? '' : 'Here is the notification description. Here is the notification description. '
                });
            },
            cancel(){
                this.formItem = this.oldobj;
            },
            //添加数据
            add(){
                if(this.path == 'users'){
                    this.modal12 = true;
                    this.titles = "添加用户"
                    this.formItem.user=this.formItem.username=this.formItem.password=this.formItem.studentId=this.formItem.name=this.formItem.sex=this.formItem.age=this.formItem.phone=this.formItem.email=this.formItem.address = "";
                }else if(this.path == 'news'){
                    this.modal12 = true;
                    this.titles = "添加新闻";
                    this.formItem.title = this.formItem.autor = this.formItem.content = "";
                }else if(this.path == 'product'){
                    this.modal12 = true;
                    this.titles = "添加产品";
                    this.formItem.productname=this.formItem.productbrief=this.formItem.retailprice=this.formItem.activityprice=this.formItem.purchaseprice=this.formItem.groupbuyingprice=this.formItem.stock=this.formItem.channel=this.formItem.category = "";
                }
            },
            //修改数据
            updata(row){
                if(this.path == 'users'){
                    this.titles = '修改用户信息'
                    this.udata(this.path,row)
                }else if(this.path == 'news'){
                    this.titles = '修改新闻信息'
                    this.udata(this.path,row)
                }else if(this.path == 'product'){
                    this.titles = '修改产品信息'
                    this.udata(this.path,row)
                }else if(this.path == 'vip'){
                    this.titles = '修改用户信息'
                    this.udata('users',row)
                }
            },
            //修改数据函数
            udata(p,row){
                this.axios({
                    url:`${this.url}${p}/${row._id}`,
                    method:'get'
                }).then(res=>{
                    this.formItem = res.data;
                    this.modal12 = true;
                    this.row = this.formItem._id
                })
            },
            removemany(){
                if(this.path == 'users'){
                    this.rm(this.path)
                }else if(this.path == 'news'){
                    this.rm(this.path)
                }else if(this.path == 'product'){
                    this.rm(this.path)
                }else if(this.path == 'vip'){
                    this.rm('users')
                }
            },
            rm(p){
                if(this.arr.length>0){
                    this.$Modal.confirm({
                        title: '确认',
                        content: '<p>你是否确认删除记录?</p>',
                        onOk: () => {
                            this.axios({
                            url:`${this.url}${p}/removes`,
                            method:'post',
                            data:{
                                ids: this.arr.toString()
                            }
                            }).then(()=>{
                            this.getdata();
                            console.log(this.$partent)
                            this.$Message.info('删除成功');
                            })
                        },
                        onCancel: () => {
                            this.$Message.info('已取消');
                        }
                    });
                }
            },
            select(data){
                var datalength = data.length;
                if(datalength>0){
                    for(var i = 0;i < datalength;i++){
                        this.arr.push(data[i]._id);
                    }
                 }
                this.arr = [...new Set(this.arr)];
            },
            checkall(res){
                var datalength = res.length;
                for(var i = 0;i < datalength;i++){
                    this.arr.push(res[i]._id);
                }
            },
            cancelall(req){
                this.arr = req;
            },
            //删除数据函数
            r(p,id){
                this.$Modal.confirm({
                    title: '温馨提示',
                    content: '<p>你确认删除吗？</p>',
                    onOk: () => {
                        this.axios({
                            url:`${this.url}${p}/${id}`,
                            method:'delete',
                        }).then( ()=>{
                            this.$Message.info('删除成功');
                            this.getdata();
                        })
                    },
                    onCancel: () => {
                        this.$Message.info('以取消');
                    }
                })
            },
            remove(id){
                if(this.path == "users"){
                    this.r(this.path,id);
                }else if(this.path == "news"){
                    this.r(this.path,id);
                }else if(this.path == "product"){
                    this.r(this.path,id);
                }else if(this.path == "vip"){
                    this.r('users',id);
                }
            },
            seatch(){
                this.getdata()
            },
            pagechange(page){
                this.page = page;
                this.getdata();
            },
            sizechange(size){
                this.rows = size;
                this.getdata();
            }
        },
        //生命周期调用获取数据方法获取数据
        mounted(){
            this.getdata();
        },

}